package Adapters;

import java.util.ArrayList;

import fiuba.descuentos.R;

import Entities.Category;
import Helpers.ContextHelper;
import Helpers.FileHelper;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;


public class CategoryAdapter extends ArrayAdapter<Category>{
	private ArrayList<Category> categorys;


	public CategoryAdapter(int textViewResourceId, ArrayList<Category> categorys) {
		super(ContextHelper.getContextInstance(), textViewResourceId, categorys);
		this.categorys = categorys;		

	}

	public View getView(int position,
			View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) ContextHelper.getContextInstance()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.category_column, null);
		}
		
		Category category = categorys.get(position);
		if (category != null) {			
			
			TextView categoryName = (TextView) v.findViewById(R.id.category_lbl_name);
			String name = category.getName();
			bindLabelWithTitle(categoryName,category.getName());

			ImageView logo = (ImageView) v.findViewById(R.id.categoryImage);
			logo.setTag(category);
			bindViewWithImage(logo ,category.getLogo());

		}

		return v;
	}


	private void bindLabelWithTitle(TextView label, String name) {
		label.setText(name);
	}

	private void bindViewWithImage(ImageView imageButton, String logo) {
		try {
			Bitmap bmp = FileHelper.getImageBitMap(logo,"");
			imageButton.setImageBitmap(bmp);
		} catch (Exception e) {

		}
	}
}